<?php
/*
 *   Pmail easy way to send emails
 *   Copyright (C) 2009  Felipe Theodoro Gonçalves (www.ftgoncalves.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *	 it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class ConfigsController extends PmailAppController {

	public $uses = array('Pmail.Config', 'Pmail.Label');
	public $components = array('Session');

	/**
	 * Carrega o controller e seta a tab correta
	 *
	 */
	public function beforeFilter(){

		$this->set('page', 'configs');

	}

	public function index(){

		$config = $this->Config->find('all');
		$this->data = $config[0];
		$this->render();

	}

	/**
	 * Configura os dados adicionais para o pleno funcionamento do sistema
	 *
	 */
	public function set_config(){

		if(isset($this->data)){

			$this->Config->id = 1;
			$this->Config->set($this->data);
			$this->Config->save();
			$this->Session->setFlash(__d('pmail', 'Data registered successfully', true), 'default', array(), 'sucesso');
			$this->redirect('/pmail');

		}else{

			$this->redirect('/pmail');

		}

	}

	/**
	 * Initial settings
	 *
	 */
	public function config(){

		$this->Config->id = 1;
		$root = $this->Config->read();

		if(($root['Config']['active'] != 1)){

			if(isset($this->data)){

				$this->data['password'] = md5($this->data['password']);
				$this->Config->id = 1;
				$this->Config->set($this->data);
				$this->Config->save();
				$this->Session->setFlash(__d('pmail', 'Root successfully configured', true), 'default', array(), 'sucesso');
				$this->redirect('/pmail');

			}

			$this->render();

		}else{

			$this->Session->setFlash(__d('pmail', 'Root already configured', true), 'default', array(), 'info');
			$this->redirect('/pmail');

		}

	}

	/**
	 * Label of used in mails
	 *
	 */
	public function labels(){

		$labels = $this->Label->find('all', array(
			'fields' => array('Label.id', 'Label.name', 'Label.description')
		));

		$this->set('labels', $labels);
		$this->render();

	}

	/**
	 * Cria os menus automatico
	 *
	 */
	public function menu(){

		$this->layout = 'blank';
		Configure::write('debug', 0);

		$links = array(
			'View configs' => array('plugin' => 'pmail', 'controller' => 'configs', 'action' => 'index'),
			'Labels' => array('plugin' => 'pmail', 'controller' => 'configs', 'action' => 'labels')
		);

		$this->set('links', $links);
		$this->render('menu/menu');

	}

}